﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

public class XmlUtil
{
    public static void WriteToXmlFile(object o, String filename)
    {
        XmlSerializer serializer = null;
        FileStream stream = null;
        try
        {
            // Create a serializer for the Sailboat type
            serializer = new XmlSerializer(o.GetType());
            // Create a new writable FileStream, using the path passed
            // as a parameter.
            stream = new FileStream(filename,
                                    FileMode.Create,
                                    FileAccess.Write);
            serializer.Serialize(stream, o /*as PlatformTypeList*/);
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }
    }

    public static object ReadFromXmlFile(Type t, String filename)
    {
        XmlSerializer serializer = null;
        FileStream stream = null;
        object sb = null;
        try
        {
            // Create a serializer for the Sailboat type
            serializer = new XmlSerializer(t);
            // Create a new readable FileStream, using the path passed as
            // a parameter
            stream = new FileStream(filename,
                                    FileMode.Open,
                                    FileAccess.Read);
            sb = serializer.Deserialize(stream);
        }
        catch (InvalidOperationException e)
        {
            throw;
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }
        return sb;
    }

    /// <summary>
    /// Method to convert a custom Object to XML string
    /// </summary>
    /// <param name="pObject">Object that is to be serialized to XML</param>
    /// <returns>XML string</returns>
    public static String serializeObject<T>(T pObject)
    {
        try
        {
            String XmlizedString = null;
            MemoryStream memoryStream = new MemoryStream();
            XmlSerializer xs = new XmlSerializer(typeof(T));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.ASCII);
            xs.Serialize(xmlTextWriter, pObject);
            memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
            XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
            return XmlizedString;
        }

        catch (Exception e)
        {
            System.Console.WriteLine(e);
            return null;
        }

    }

    /// <summary>
    /// Method to convert a custom Object to XML string
    /// </summary>
    /// <param name="pObject">Object that is to be serialized to XML</param>
    /// <returns>XML string</returns>
    public static String serializeObject(object pObject)
    {
        try
        {
            String XmlizedString = null;
            MemoryStream memoryStream = new MemoryStream();
            XmlSerializer xs = new XmlSerializer(pObject.GetType());
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.ASCII);
            xs.Serialize(xmlTextWriter, pObject);
            memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
            XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
            return XmlizedString;
        }

        catch (Exception e)
        {
            System.Console.WriteLine(e);
            return null;
        }

    }

    /// <summary>
    /// Method to reconstruct an Object from XML string
    /// </summary>
    /// <param name="pXmlizedString"></param>
    /// <returns></returns>
    public static T deserializeObject<T>(String pXmlizedString)
    {

        XmlSerializer xs = new XmlSerializer(typeof(T));
        // MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
        // XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
        // return (T) xs.Deserialize(memoryStream);
        MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.ASCII);
        return (T)xs.Deserialize(memoryStream);
        // return (T)xs.Deserialize(xmlTextWriter);
    }


    /// <summary>
    /// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
    /// </summary>
    /// <param name="characters">Unicode Byte Array to be converted to String</param>
    /// <returns>String converted from Unicode Byte Array</returns>
    private static string UTF8ByteArrayToString(byte[] characters)
    {
        // UTF8Encoding encoding = new UTF8Encoding();
        ASCIIEncoding encoding = new ASCIIEncoding();
        string constructedString = encoding.GetString(characters);
        return (constructedString);
    }

    /// <summary>
    /// Converts the String to UTF8 Byte array and is used in De serialization
    /// </summary>
    /// <param name="pXmlString"></param>
    /// <returns></returns>
    private static Byte[] StringToUTF8ByteArray(string pXmlString)
    {
        // UTF8Encoding encoding = new UTF8Encoding();
        ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] byteArray = encoding.GetBytes(pXmlString);
        return byteArray;
    }



}